/*
 * Copyright (c) 2011 Shanda Corporation. All rights reserved.
 *
 * Created on 2012-06-11.
 */

package com.whq.knowverywell.task;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Environment;

import com.whq.knowverywell.util.LogUtil;
import com.whq.knowverywell.util.constant.CfgConstant;

/**
 * 升级Task.
 *
 * @author wanghuaiqiang
 */
public class UpdateTaskOld extends AsyncTask<String, String, String> {
    public static final String TAG = UpdateTaskOld.class.getSimpleName();
    
    private static final String APK_LOCAL_DIR = Environment.getExternalStorageDirectory() + "/Download";
    
    private Context ctx;
    private ProgressDialog pBar;
    private String apkName;
    
    public UpdateTaskOld(Context ctx, String apkName) {
        this.ctx = ctx;
        this.apkName = apkName;
    }
    
    @Override
	protected void onPreExecute() {
		super.onPreExecute();
		
		pBar = new ProgressDialog(ctx);  
        pBar.setTitle("正在下载安装文件");  
        pBar.setMessage("请稍候...");  
        pBar.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        pBar.show();
	}

	@Override
    protected String doInBackground(String... params) {
		// 创建目录
		try {
            File fileDir = new File(APK_LOCAL_DIR);
            fileDir.mkdirs();
        } catch (Exception e) {
            LogUtil.e(TAG, "make dir [" + APK_LOCAL_DIR + "] error: ", e);
        }
		
        String url = CfgConstant.SERVER_HOST + CfgConstant.SERVER_CONTEXT + "/apk/" + this.apkName;
        
        LogUtil.i(TAG, "apk url: " + url);
        
        HttpClient client = new DefaultHttpClient();  
        HttpGet get = new HttpGet(url);  
        HttpResponse response;  
        try {  
            response = client.execute(get);  
            HttpEntity entity = response.getEntity();  
            InputStream is = entity.getContent();  
            FileOutputStream fileOutputStream = null;  
            if (is != null) {  
                File file = new File(APK_LOCAL_DIR, apkName);  
                fileOutputStream = new FileOutputStream(file);  
                byte[] buf = new byte[1024];  
                int ch = -1;  
                while ((ch = is.read(buf)) != -1) {  
                    fileOutputStream.write(buf, 0, ch);  
                }  
            }  
            fileOutputStream.flush();  
            if (fileOutputStream != null) {  
                fileOutputStream.close();  
            }  
        } catch (Exception e) {  
            LogUtil.e(TAG, "下载Apk出错: ", e) ;
        }  
        return null;
    }
    
    @Override
    protected void onPostExecute(String result) {
    	pBar.cancel(); 

    	Intent intent = new Intent(Intent.ACTION_VIEW);  
        intent.setDataAndType(Uri.fromFile(new File(APK_LOCAL_DIR, apkName)),  "application/vnd.android.package-archive");  
        this.ctx.startActivity(intent);  
    }
}
